// app_main.c
//

#include "itc.h"
#include "sm_trace.h"
#include "sm_kapi.h"
#include "session.h"
#include "physical.h"
#include "trans.h"
#include "link.h"
#include "app_proxy.h"
#include "app_main.h"
#include "mmi.h"
#include"ci.h"


#define TRMOD_MAIN	TRMOD_USR0

unsigned long ti_taskID;
unsigned long ph_taskID;
unsigned long li_taskID;
unsigned long tr_taskID;
unsigned long se_taskID;
unsigned long app_taskID;
unsigned long phread_taskID;

int start_alltask(void)
{
	SM_TRACE_ENTRY(TRMOD_MAIN, "start_alltask() - entry\r\n");

	//gettimeofday(&g_startTime, NULL);
	g_startTime = SM_OsMsecCountGet();

	SM_TRACE_INFO(TRMOD_MAIN, "start_alltask() - create physical task...\r\n");
	ph_taskID = SM_CreateTask("physical", physical, NULL, NULL, 5*1024, CITASK_PRIORITY, TRUE, PHYSICAL);

	phread_taskID = SM_CreateTask("physical_read", physical_read, NULL, NULL, 5*1024, 80, TRUE, PHYREAD_TASKAID);

	SM_TRACE_INFO(TRMOD_MAIN, "start_alltask() - create link task...\r\n");
	li_taskID = SM_CreateTask("link", link, NULL, NULL, 5*1024, CITASK_PRIORITY, TRUE, LINK);

	SM_TRACE_INFO(TRMOD_MAIN, "start_alltask() - create transport task...\r\n");
	tr_taskID = SM_CreateTask("transport", transport, NULL, NULL, 5*1024, CITASK_PRIORITY, TRUE, TRANS);

	SM_TRACE_INFO(TRMOD_MAIN, "start_alltask() - create session task...\r\n");
	se_taskID = SM_CreateTask("session", session, NULL, NULL, 5*1024, CITASK_PRIORITY, TRUE, SESS);

	SM_TRACE_INFO(TRMOD_MAIN, "start_alltask() - create rm task...\r\n");
	app_taskID = SM_CreateTask("rm", appproxy_task, NULL, NULL, 5*1024, CITASK_PRIORITY, TRUE, RM);

	//SM_TRACE_INFO(TRMOD_MAIN, "start_alltask() - create apptest task...\r\n");
	//test_taskID = SM_CreateTask("apptest", hc_test_task, NULL, NULL, 5*1024, CITASK_PRIORITY, TRUE, LSC_RCV);

	SM_TRACE_RETURN(TRMOD_MAIN, "start_alltask() - return\r\n");
	return 0;
}

static pfCiDebug pfDebugOut = NULL;
static pfCiCallback pfCallback = NULL;

void CIDebug(unsigned int u32Level, const char* ps8String)
{
	if(pfDebugOut)
	{
		if(u32Level == SM_DEBUG_COMMON)
			pfDebugOut(CI_DEBUG_LEVEL_COMMON, ps8String);
		else if(u32Level == SM_DEBUG_ERROR)
			pfDebugOut(CI_DEBUG_LEVEL_ERROR, ps8String);
	}
}

void CI_RegistorDebugFun(pfCiDebug pfCiDebugFun)
{
	pfDebugOut = pfCiDebugFun;
	SM_RegistorDebugFun(CIDebug);
}

int CI_Callback(PHY_STATUS_T eStatus)
{
	if(pfCallback)
	{
		if(eStatus == PHY_STATUS_FAILED)
			pfCallback(CI_CALLBACK_CONNECTION_FAILED);
	}
	return 0;
}

void CI_RegistorCiCallback(pfCiCallback pfCiCallbackFun)
{
	pfCallback = pfCiCallbackFun;
}

int ci_init()
{
	SM_TRACE_ENTRY(TRMOD_MAIN, "main() - entry\r\n");
//	SM_SetPrintFileName("trace_host.log");
	SM_RegistorDebugFun(CIDebug);
	SM_TRACE_SET_LEVEL(TRMOD_MAIN, TRLEV_ERROR);
	SM_TRACE_SET_LEVEL(TRMOD_APP, TRLEV_ERROR);
	SM_TRACE_SET_LEVEL(TRMOD_SESS, TRLEV_ERROR);
	SM_TRACE_SET_LEVEL(TRMOD_MMI, TRLEV_NOTIFY);
	SM_TRACE_SET_LEVEL(TRMOD_SAS, TRLEV_NOTIFY);
	SM_TRACE_SET_LEVEL(TRMOD_CAS, TRLEV_NOTIFY);
	SM_TRACE_SET_LEVEL(TRMOD_HC, TRLEV_NOTIFY);
	
	SM_TRACE_SET_LEVEL(TRMOD_TRANS, TRLEV_ERROR);
	SM_TRACE_SET_LEVEL(TRMOD_LINK, TRLEV_ERROR);
	SM_TRACE_SET_LEVEL(TRMOD_PHY, TRLEV_ERROR);
	SM_TRACE_SET_LEVEL(TRMOD_DRV, TRLEV_ERROR);

	PHYSICAL_RegistorErrorCallback(CI_Callback);
	SM_TRACE_INFO(TRMOD_MAIN, "main() - create itc_timer_thread\r\n");
	ti_taskID = SM_CreateTask("itc_timer_thread", itc_timer_thread, NULL, NULL, 5*1024, CITASK_PRIORITY, TRUE, ITC_TIMER);
	start_alltask();

	unsigned int hThreadId;
	SM_OsThreadCreate(0, DVBUpDataCIData, NULL, &hThreadId);
	
	return 0;	
}

void SM_TaskInfo(void)
{
	int i = 0;
	SM_TRACE_CRITICAL(TRMOD_APP, "*****************Task info*******************\n");
	for (i = 0; i < SM_MAX_TASK_COUNT; i++)		
	{
		if (g_arTasks[i].m_taskId == 0)
		{
			continue;
		}
		else
		{
			SM_TRACE_CRITICAL(TRMOD_APP, "Task id: %d\tTask aid: %d\tTask name: %s\n", g_arTasks[i].m_taskId, g_arTasks[i].m_taskAid, g_arTasks[i].m_szName);
		}
	}
	SM_TRACE_CRITICAL(TRMOD_APP, "*****************Task info end*******************\n");
	return;
}

